<?php

declare(strict_types=1);

namespace App\Service;

use App\Common\Tools\DbTools;
use App\Constants\ErrorCode;
use App\Library\Common;
use App\Model\FlyShop;
use App\Model\FlyShopOpenTime;
use App\Model\ReservedSeatsSet;
use App\Service\ShopConfigService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;


class ReservedSeatsService extends BaseService
{

    /**
     * @Inject
     * @var ShopConfigService
     */
    private $shopConfig;

    public function info(int $id)
    {
//        $distance = DbTools::getPointDistance(121.73756, 34.74014, 120.73756, 30.74014);
//        // 范围
//        $arr = FlyShop::query()->whereRaw("st_distance_sphere(point(120.73756,30.74014),point(lng,lat)) > 4000")
//            ->select(['ids', Db::raw("st_distance_sphere(point(120.73756,30.74014),point(lng,lat)) as distance")])
//            ->get()->toArray();
//        return [
//            'distance' => $distance,
//            'min_distance' => $arr
//        ];

        return ReservedSeatsSet::query()
            ->where('shop_id',$id)
//            ->whereJsonLength('open_time', '1')
            ->first();
    }

    public function index($param)
    {
        $diff_days = 0;$juli_shop_ids = [];$shop_ids=[];
        if(isset($param['date'])&&$param['date']!=''){
            $diff_days = abs(Common::diff_data(date('Y-m-d'),$param['date']));
        }
        $lat = floatval($param["lat"]);
        $lng = floatval($param["lng"]);
        if(isset($param['range'])&&$param['range']!=''){
            $meter = strval($param['range']*1000);
            $juli_shop_ids = FlyShop::query()
                ->whereRaw("st_distance_sphere(point($lng,$lat),point(lng,lat)) > $meter")
                ->where("food_state",1)
                ->where("food_switch",1)
                ->pluck('id')->toArray();
        }
        $set =  New ReservedSeatsSet();
        if(isset($param['time'])&&$param['time']!=''){
           $shop_ids = FlyShopOpenTime::query()
                ->where('start_time','>=',$param['time'])
                ->where('end_time','<=',$param['time'])->pluck('shop_id')->toArray();
        }
        return  $set->when((isset($param['is_unshared'])&&$param['is_unshared']!=''),function ($query) use($param){
                $query->where('is_unshared',$param['is_unshared']);
            })
            ->when((isset($param['date'])&&$param['date']!=''),function ($query) use($diff_days){
                $query->where('advance_days','>=',$diff_days);
            })
            ->when((isset($param['range'])&&$param['range']!=''),function ($query) use($juli_shop_ids){
                $query->whereIn('shop_id',$juli_shop_ids);
            })
            ->when((isset($param['time'])&&$param['time']!=''),function ($query) use($shop_ids){
                $query->whereIn('shop_id',$shop_ids);
            })
            ->with('food_shop_info')
            ->select()
            ->get()->toArray();
    }

    /**
     * 设置
     * @param array $param
     * @return bool|int
     */
    public function set_seat(array $param)
    {
        if(isset($param['open_time'])){
            if($param['open_time']!=[]){
                $open_time = $param['open_time'];
                $check_res  = $this->shopConfig->check_time($open_time);
                if($check_res){
                    $this->shopConfig->set_open_time(intval($param['shop_id']),$open_time);
                }else{
                    throw new \RuntimeException("时间格式不正确！", ErrorCode::SERVER_ERROR);
                }
            }
        }
        var_dump($param);die;
        return ReservedSeatsSet::query()->insertGetId(Common::filter_null($param));
    }

    /**
     * 设置
     * @param array $param
     * @return bool|int
     */
    public function update_seat(array $param)
    {
        if(isset($param['open_time'])){
            if($param['open_time']!=[]){
                $open_time = $param['open_time'];
                $param['open_time'] = json_encode($param['open_time']);
                $check_res  = $this->shopConfig->check_time($open_time);
                if($check_res){
                    $this->shopConfig->set_open_time(intval($param['shop_id']),$open_time);
                }else{
                    throw new \RuntimeException("时间格式不正确！", ErrorCode::SERVER_ERROR);
                }
            }
        }
        ReservedSeatsSet::query()
            ->where('id',$param['id'])
            ->where('shop_id',$param['shop_id'])
            ->update(Common::filter_null($param));
        return true;
    }

    /**
     * @param int $set_id
     * @param array $open_time
     * @param int $shop_id
     * @return bool|int
     */
    public function set_open_time(int $set_id,array $open_time,int $shop_id)
    {
        $time_arr = [];
        foreach ($open_time as $key=>$value){
            $time_arr[] = [
                'start_time'=>$value['start_time'],
                'end_time'=>$value['end_time'],
                'set_id'=>$set_id,
                'shop_id'=>$shop_id,
            ];
        }
        $ReservedSeatsTime =  New  ReservedSeatsTime();
        $ReservedSeatsTime
            ->where('set_id',$set_id)
            ->where('shop_id',$shop_id)
            ->delete();
        if(count($time_arr)>0){
            $ReservedSeatsTime->insert($time_arr);
        }
    }

//    获取支持订订座的店铺
    public function get_reserved_shop()
    {
        return ReservedSeatsSet::query()
            ->where('status',1)
            ->pluck('shop_id')->toArray();
    }



}